<?php
namespace app\shop\controller;
use app\AdminRest;
use app\shop\model\AdminGoods;
use app\shop\model\AdminShopSpePrice;
use app\shop\model\BargainAtvPv;
use app\shop\model\BargainConfig;
use app\shop\model\BargainRecord;
use app\shop\model\BargainRecordList;
use app\shop\model\PurchaseCate;
use app\shop\model\PurchaseCompany;
use think\App;
use app\shop\model\BargainAtv as Model;

use think\facade\Db;

class AdminBargain extends AdminRest
{


    protected $model;

    protected $config_model;

    protected $record_model;

    protected $pv_model;

    protected $record_list_model;




    public function __construct(App $app) {

        parent::__construct($app);

        $this->model = new Model();

        $this->config_model = new BargainConfig();

        $this->record_model = new BargainRecord();

        $this->record_list_model = new BargainRecordList();

        $this->pv_model = new BargainAtvPv();


    }


    /**
     * @author chenniang
     * @DataTime: 2020-06-22 09:34
     * @功能说明:活动列表
     */
    public function atvList(){

        $input = $this->_param;
        //过期的自动下架
        $this->model->where(['status'=>1])->where('end_time','<',time())->update(['status'=>0]);

        $dis[] = ['uniacid','=',$this->_uniacid];
        //未删除
        $dis[] = ['status','>',-1];

        if(!empty($input['title'])){

            $dis[] = ['title','like','%'.$input['title'].'%'];
        }

        if(!empty($input['status'])){

            if($input['status']==1){

                $dis[] = ['start_time','>',time()];
            }

            if($input['status']==2){

                $dis[] = ['start_time','<',time()];

                $dis[] = ['end_time','>',time()];

            }

            if($input['status']==3){

                $dis[] = ['end_time','<',time()];

            }

        }

        $data = $this->model->dataList($dis,$input['limit']);

        if(!empty($data['data'])){

            foreach ($data['data'] as &$v){

                $dis = [

                    'atv_id' => $v['id'],

                ];
                //浏览量
                $v['pv'] = $this->pv_model->where($dis)->sum('num');
                //访客数
                $v['user_count'] = $this->pv_model->where($dis)->count();
                //发起数
                $v['record_count'] = $this->record_model->where($dis)->count();
                //帮砍人数
                $v['record_user_count'] = $this->record_list_model->where($dis)->count();
                //成功数
                $v['record_success_count'] = $this->record_model->where($dis)->where('status','>',1)->count();
                //成单数
                $v['order_count'] = $this->record_model->where($dis)->where('status','=',3)->count();
                //剩余库存
                $v['y_stock']     = array_sum(array_column($v['spe'],'true_stock'));

            }
        }

        return $this->success($data);

    }

    /**
     * @author chenniang
     * @DataTime: 2020-12-09 14:35
     * @功能说明:活动详情
     */
    public function atvInfo(){

        $input = $this->_param;

        $data  = $this->model->dataInfo(['id'=>$input['id']]);

        $goods_model = new AdminGoods();

        $data['goods_name'] = $goods_model->where(['id'=>$data['goods_id']])->value('name');

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-12-09 15:19
     * @功能说明:添加
     */
    public function atvAdd(){

        $input = $this->_input;

        $input['uniacid'] = $this->_uniacid;

        $goods_info = $this->model->where(['goods_id'=>$input['goods_id']])->where('status','>',0)->find();

        if(!empty($goods_info)){

            $this->errorMsg('该商品已有上架的砍价活动');
        }


        $res = $this->model->dataAdd($input);

        if(!empty($res['code'])){

            $this->errorMsg($res['msg']);
        }

        return $this->success($res);
    }



    /**
     * @author chenniang
     * @DataTime: 2020-12-09 15:20
     * @功能说明:编辑
     */
    public function atvUpdate(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id'],

        ];

        $input['uniacid'] = $this->_uniacid;

        $goods_info = $this->model->where(['goods_id'=>$input['goods_id']])->where('id','<>',$input['id'])->where('status','>',0)->find();

        if(!empty($goods_info)){

            $this->errorMsg('该商品已有上架的砍价活动');
        }

        $res = $this->model->dataUpdate($dis,$input);

        if(!empty($res['code'])){

            $this->errorMsg($res['msg']);
        }

        return $this->success($res);


    }


    /**
     * @author chenniang
     * @DataTime: 2020-12-09 15:20
     * @功能说明:编辑
     */
    public function atvStatusUpdate(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']
        ];

        $goods_id   = $this->model->where($dis)->value('goods_id');

        $goods_info = $this->model->where(['goods_id'=>$goods_id])->where('id','<>',$input['id'])->where('status','>',0)->find();

        if(!empty($goods_info)&&$input['status']==1){

            $this->errorMsg('该商品已有上架的砍价活动');
        }

        $data = $this->model->dataInfo($dis);

        if($input['status']==1&&$data['end_time']<time()){

            $this->errorMsg('活动已结束无法上架');

        }
        //总库存
        $all_stock = array_sum(array_column($data['spe'],'true_stock'));

        if($input['status']==1&&$all_stock<=0){

            $this->errorMsg('活动无库存无法上架');

        }

        $res = $this->model->where($dis)->update(['status'=>$input['status']]);

        return $this->success($res);


    }


    /**
     * @author chenniang
     * @DataTime: 2021-01-19 11:04
     * @功能说明:配置详情
     */
    public function configInfo(){

        $dis = [

            'uniacid' => $this->_uniacid
        ];

        $data = $this->config_model->dataInfo($dis);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-01-19 11:09
     * @功能说明:配置编辑
     */
    public function configUpdate(){

        $input = $this->_input;

        $dis = [

            'uniacid' => $this->_uniacid
        ];

        $data = $this->config_model->dataUpdate($dis,$input);

        return $this->success($data);
    }


    /**
     * @author chenniang
     * @DataTime: 2021-01-19 15:08
     * @功能说明:活动参与详情 1进行中 2成功 3 购买 4订单超时取消 5购买取消 -1 失败 -2 用户手动取消失败
     */
    public function recordList(){

        $input = $this->_input;

        $dis[] = ['a.uniacid','=',$this->_uniacid];

        if(!empty($input['atv_id'])){

            $dis[] = ['a.atv_id','=',$input['atv_id']];
        }

        if(!empty($input['status'])){

            if($input['status']==-1){

                $dis[] = ['a.status','in',[-2,-1]];

            }elseif($input['status']==2){

                $dis[] = ['a.status','in',[2,3,4,5]];

            }else{

                $dis[] = ['a.status','=',$input['status']];
            }

        }

        $data = $this->record_model->dataList($dis,$input['limit']);

        return $this->success($data);


    }












}
